<template>
  <div class="ordersBox">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/users' }">订单管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/users' }">订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- @blur="getSearchUsers" -->
    <!-- 用户列表主体 -->
    <el-card class="box-card">
      <!-- 搜索框和添加用户按钮 -->
      <el-row type="flex">
        <el-col :span="8">
          <div class="grid-content bg-purple title" style="text-align: left">
            <el-input
              clearable
              placeholder="请输入内容"
              v-model.trim="params.query"
              class="input-with-select"
              @clear="getSearchOrders"
              @keydown.native.enter="getSearchOrders"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getSearchOrders"
              ></el-button>
            </el-input>
          </div>
        </el-col>
      </el-row>
      <!-- 用户列表-->
      <el-table :data="tableData" border style="width: 100%  margin-top:10px">
        <el-table-column type="index" label="#" width="50"> </el-table-column>

        <el-table-column prop="order_number" label="订单编号" align="center">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格" align="center">
        </el-table-column>
        <el-table-column prop="pay_status" label="是否付款" align="center">
          <template slot-scope="scope">
            <el-tag type="danger" effect="dark" v-if="scope.row.pay_status == 0"
              >未付款</el-tag
            >
            <el-tag effect="dark" v-else-if="scope.row.pay_status == 1"
              >已付款</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" align="center">
        </el-table-column>
        <el-table-column prop="create_time" label="下单时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.create_time | formDate }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              content="修改订单信息"
              placement="top"
            >
              <el-button
                size="mini"
                type="primary"
                @click="handleEdit(scope.$index, scope.row)"
                icon="el-icon-edit"
              >
              </el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="物流信息"
              placement="top"
            >
              <el-button
                size="mini"
                type="success"
                @click="handleLook(scope.$index, scope.row)"
                icon="el-icon-location"
              >
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!--  分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="params.pagenum"
      :page-sizes="[1, 2, 5, 8]"
      :page-size="params.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalpage"
    >
    </el-pagination>
    <!-- 修改地址弹框 -->
    <el-dialog title="修改地址" :visible.sync="addressFromDialog">
      <el-form :model="addressform" :rules="rules">
        <el-form-item
          label="省市区/县"
          :label-width="formLabelWidth"
          prop="address1"
        >
          <el-cascader
            v-model="addressform.address1"
            :options="cityData"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item
          label="详细地址"
          :label-width="formLabelWidth"
          prop="address2"
        >
          <el-input
            v-model="addressform.address2"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDecidedAddress">取 消</el-button>
        <el-button type="primary" @click="decidedAddress">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 物流信息弹框 -->
    <el-dialog title="物流进度" :visible.sync="postManFromDialog">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in postManFrom"
          :key="index"
          :icon="activity.icon"
          :type="activity.type"
          :size="activity.size"
          :color="activity.color"
          :timestamp="activity.ftime"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelPostManFromDialog">取 消</el-button>
        <el-button type="primary" @click="decidePostManFromDialog"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getOrders, getAddress ,getList} from "../../api/index";
import cityData from "./citydata.js"; //物流信息直接从本地拿到的

export default {
  name: "", //keep-alive 使用
  data() {
    return {
      cityData,
      tableData: [],
      totalpage: 0, //总页数
      //分页
      params: {
        query: "",
        pagenum: 1,
        pagesize: 5, //页面条数
      },

      formLabelWidth: "100px",

      // 修改订单弹框数据
      addressFromDialog: false,
      addressform: {
        address1: "",
        address2: "",
      },
      // 查看物流信息弹框
      postManFromDialog: false,
      postManFrom: [],
      // 验证规则
      rules: {
        address1: [
          { required: true, message: "请输入省市区", trigger: "blur" },
        ],
        address2: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
      },
     order_id:0,//订单编号id初始化
    };
  },
  created() {
    this.getOrdersList();
     this.getCurrentIdList();
  },
  methods: {
    //初始化获取订单列表
    async getOrdersList() {
      let {
        data: { goods, pagenum, total },
      } = await getOrders(this.params);

      this.tableData = goods;
      this.params.pagenum = +pagenum;
      this.totalpage = total;
    },
    //分页功能
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.params.pagesize = val;
      this.getOrdersList();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.params.pagenum = val;
      this.getOrdersList();
    },

    //搜索功能
    // 根据id获取相应的订单
    async getCurrentIdList(){
     
    },
    getSearchOrders() {
      this.getCurrentIdList();
    },

    // 修改订单信息
    handleEdit() {
      this.addressFromDialog = true;
    },
    handleChange(value) {
      console.log(value);
    },
    cancelDecidedAddress() {
      this.addressFromDialog = false;
      this.addressform = {};
    },
    decidedAddress() {
      this.addressFromDialog = false;
      this.addressform = {};
    },
    // 查看物流信息
    async handleLook() {
      this.postManFromDialog = true;
      let {
        data,
        meta: { status },
      } = await getAddress({ id: 1106975712662 });
      if (status != 200) return this.$message.errer("获取失败");
      // 进行数据处理 添加样式
      for (var i = 0; i < data.length; i++) {
        let obj = { icon: "el-icon-location", color: "#0bbd87" };
        data[i] = { ...data[i], ...obj };
      }
      this.postManFrom = data;
    },

    // 关闭弹框
    cancelPostManFromDialog() {
      this.postManFromDialog = false;
    },
    decidePostManFromDialog() {
      this.postManFromDialog = false;
    },
  },
};
</script>

<style scoped>
.box-card {
  width: 800px;
}

.ordersBox {
  padding-left: 20px;
}

.el-table {
  margin-top: 20px;
}
.el-card__body {
  width: 800px !important;
}
.el-breadcrumb {
  background-color: #fff;
  padding-left: 20px;
}

.el-pagination {
  margin: 20px auto;
  text-align: left;
  padding-left: 30px;
}

.el-dialog__body {
  padding: 30px 86px 21px 4px;
}
.el-cascader {
  width: 100%;
}
</style>